package dao

import (
	"blog/models"
	"log"
)

func GetAllCategory() ([]models.Category, error) {
	rows, err := DB.Query("select * from blog_category")
	if err != nil {
		log.Println("GetAllCategory查询失败", err)
		return nil, err
	}
	var categorys []models.Category
	for rows.Next() {
		var category models.Category
		err := rows.Scan(&category.Cid, &category.Name, &category.CreateAt, &category.UpdateAt)
		if err != nil {
			log.Println("GetAllCategory取值失败", err)
			return nil, err
		}
		categorys = append(categorys, category)
	}
	return categorys, nil
}
func GetCategoryNameById(CategoryId int) (string, error) {
	row := DB.QueryRow("select * from blog_category where Cid = ?", CategoryId)
	if row.Err() != nil {
		log.Println("GetCategoryNameById查询失败", row.Err())
		return "", row.Err()
	}

	var category models.Category
	err := row.Scan(&category.Cid, &category.Name, &category.CreateAt, &category.UpdateAt)
	if err != nil {
		log.Println("GetCategoryNameById取值失败", err)
		return "", err
	}
	return category.Name, err
}
